package net.zdreamer.geracomium.utils.httputils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;

import net.zdreamer.geracomium.application.BaseApplication;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/1/20.
 */
public class AssessService {

    /**
     * StringRequest
     *
     * @param url
     * @param handler
     */
    public static void AssessRequest( String url, final Handler handler) {
        final Message msg = new Message();
        RequestQueue requestQueue = BaseApplication.get().getRequestQueue();
        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
//                        try {
//                            JSONArray jsonArray = new JSONArray(response);
//                            for (int i = 0; i < jsonArray.length(); i++) {
//                                JSONObject jsonObject = new JSONObject(jsonArray.getString(i));
//                                String ss = jsonObject.getString("title");
//                                String id = jsonObject.getString("id");
//                                System.out.println(jsonObject.getString("title") + "   "+id);
//                            }
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                        System.out.println(response);
                        msg.arg1 = 0;
                        msg.obj = response;
                        handler.sendMessage(msg);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("请求失败+" + error.getMessage());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", BaseApplication.get().getToken());
                // System.out.println(headers);
                // MyLog.d(TAG, "headers=" + headers);
                return headers;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                // TODO Auto-generated method stub
                String str = null;
                try {
                    str = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return Response.success(str, HttpHeaderParser.parseCacheHeaders(response));
            }
        };

        requestQueue.add(stringRequest);


    }
}
